/**
 * Created by 追风少年 on 2018/6/7.
 */

var RoleInput = {
    URL:{
        ADD_URL:'/admin/role/add',
        UPDATE_URL:'/admin/role/update',
        FIND_ONE_URL:"/admin/role/findById",
        GET_MENU_TREE_URL:"/admin/role/findMenuTree"
    }
};

var setting = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

function setCheck() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
        type = { "Y" : "ps", "N" : "ps" };
}

app.controller('roleInputController', function($scope,$http) {

    $scope.flowTypeList = [];

    $scope.entity = {
        rolename:"",
        menuid:""
    };
    $scope.submitUrl = RoleInput.URL.ADD_URL;

    /**
     * 初始化所有菜单Tree
     */
    $scope.initAllMenuTree = function(){
        $http.post(RoleInput.URL.GET_MENU_TREE_URL,{}).success(function(data,status,headers,config){
            console.log("数据请求成功");
            console.log(data);
            if(data.status){
                var zNodes = data.data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                setCheck();
                $("#py").bind("change", setCheck);
                $("#sy").bind("change", setCheck);
                $("#pn").bind("change", setCheck);
                $("#sn").bind("change", setCheck);
            }else{
                console.error(data);
                alert("数据加载失败,请联系管理员");
            }
        }).error(function(data,status,headers,config){
            console.error(data);
        });
    };

    // 页面加载完成
    $scope.load = function(){
        $scope.initAllMenuTree();
        var id = $("#roleId").val();
        if(id != "none"){ // is update page
            $scope.submitUrl = RoleInput.URL.UPDATE_URL;
            $http.post(RoleInput.URL.FIND_ONE_URL,{id:id}).success(function(data,status,headers,config){
                console.log("数据请求成功");
                console.log(data);
                if(data.status){
                    var menulistResult = JSON.parse(data.data.menulistResult);
                    if(menulistResult.status){
                        var menulist = menulistResult.data;
                        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                        // 默认选中data.data中的项
                        for(var i=0;i<menulist.length;i++){
                            //checkNode需要保证此节点数据对象 是 zTree 内部的数据对象
                            // 所以我这里根据ID取zTree里面找出来的 ε=( o｀ω′)ノ
                            zTree.checkNode(zTree.getNodeByParam("id", menulist[i].menuid, null), true, false);
                        }
                    }else{
                        console.error(menulistResult);
                        alert("角色菜单数据加载失败,请联系管理员");
                        return false;
                    }
                    var roleInfoResult = JSON.parse(data.data.roleInfoResult);
                    if(roleInfoResult.status){
                        $scope.entity = roleInfoResult.data;
                    }else{
                        console.error(menulistResult);
                        alert("角色菜单数据加载失败,请联系管理员");
                        return false;
                    }
                }else{
                    console.error(data);
                    alert("数据加载失败,请联系管理员");
                }
            }).error(function(data,status,headers,config){
                console.error(data);
            });
        }
        // $scope.initValue();
    };

    // 获取服务类型列表 + 初始化选择的值
    $scope.initValue = function(){
        $http.post(RoleInput.URL.GET_TYPE_LIST_URL,{}).success(function(data,status,headers,config){
            if(data.status){
                $scope.flowTypeList = data.data;
                var checkBoxDiv = $("#serviceTypeDiv");
                var selectedFlowTypeArr = [];
                if(!Utils.isNull($scope.entity.flow_type)){
                    selectedFlowTypeArr = $scope.entity.flow_type.split(",");
                }
                for(var i = 0; i < $scope.flowTypeList.length; i++){
                    var checked = false;
                    var type = $scope.flowTypeList[i];
                    for (var j = 0; j < selectedFlowTypeArr.length; j++){
                        if(selectedFlowTypeArr[j] == type.namecode){
                            checked = true;
                            break;
                        }
                    }
                    if(checked){
                        checkBoxDiv.append('<input checked="" value="'+type.namecode+'" type="checkbox" name="serviceType" title="'+type.name+'">');
                    }else{
                        checkBoxDiv.append('<input value="'+type.namecode+'" type="checkbox" name="serviceType" title="'+type.name+'">');
                    }
                }
                layui.use('form', function(){
                    var form = layui.form;
                    //各种基于事件的操作，下面会有进一步介绍
                    form.render('checkbox');
                    form.val("inputForm", {

                    });
                });
            }else{
                console.error(data);
                alert("数据加载失败,请联系管理员");
            }
        }).error(function(data,status,headers,config){
            console.error(data);
        });
    };

    $scope.validateParam = function(){

        if(Utils.isNull($scope.entity.rolename)){
            layer.msg("请填写角色名称");
            return false;
        }
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = zTree.getCheckedNodes(true); // 获取勾选的节点
        if(nodes.length == 0){
            layer.msg("请选择角色权限");
            return false;
        }
        $scope.entity.menuid = "";
        for (var i = 0; i < nodes.length; i++){
            if(i>0){
                $scope.entity.menuid += ",";
            }
            $scope.entity.menuid += nodes[i].id;
        }
        return true;
    };

    $scope.submit = function($event){
        if(!$scope.validateParam()){ // 校验参数
            return false;
        }
        var btn = $event.target;
        btn.disabled = true;
        $http.post($scope.submitUrl,$scope.entity).success(function (ret) {
            console.log(ret);
            if(ret.status){
                layer.alert(ret.msg, {
                    icon: 1,
                    skin: 'layer-ext-moon',
                    yes:function(index){
                        layer.close(index);
                        parent.closeWinAndFlush();
                    }
                });
            }else{
                btn.disabled = false;
                return;
            }
        }).error(function(ret){
            btn.disabled = false;
            console.error("err:"+ret);
        });

    };

});